import com.dinmax.flash.draw.control.button.*;

class PressButtonViewClassic implements PressButtonView {

	private var borde:Rectangulo;
	private var textField:TextField;
	private var hoverFormat:PressButtonViewItem;
	private var normalFormat:PressButtonViewItem;
	private var pressFormat:PressButtonViewItem;
	private var disableFormat:PressButtonViewItem;

	private var COLOR_BG:Number=0xe9e9e9;
	private var ALPHA_BG:Number=100;
	private var WIDTH_BORDER:Number=2;
	private var COLOR_BORDER:Number=0x0000FF;
	private var ALPHA_BORDER:Number=15;
	private var COLOR_DISABLED:Number=0xeeeeee;
	private var HEIGHT_TEXT:Number;
	private var BASELINE:Number;
	private var HTML:Boolean=true;
	private var MARGIN:Number=0;
	
	private var HOVER_FONT:String="Tahoma";
		private var HOVER_SIZE:Number=11;
		private var HOVER_COLOR:Number=0;
		private var HOVER_BOLD:Boolean=false;
		private var HOVER_ITALIC:Boolean=false;
		private var HOVER_ALIGN:String="center";
		private var HOVER_BG:Boolean=true;
		private var HOVER_BG_COLOR:Number=0x0000FF;
		private var HOVER_BG_ALPHA:Number=7;
		private var HOVER_LEFT_INDENT:Number=0;
	private var NORMAL_FONT:String="Tahoma";
		private var NORMAL_SIZE:Number=11;
		private var NORMAL_COLOR:Number=0x3e3e3e;
		private var NORMAL_BOLD:Boolean=false;
		private var NORMAL_ITALIC:Boolean=false;
		private var NORMAL_ALIGN:String="center";
		private var NORMAL_BG:Boolean=true;
		private var NORMAL_BG_COLOR:Number=0xeeeeFF;
		private var NORMAL_BG_ALPHA:Number=55;
		private var NORMAL_LEFT_INDENT:Number=0;
	private var PRESS_FONT:String="Tahoma";
		private var PRESS_SIZE:Number=11;
		private var PRESS_COLOR:Number=0x0000AA;
		private var PRESS_BOLD:Boolean=false;
		private var PRESS_ITALIC:Boolean=false;
		private var PRESS_ALIGN:String="center";
		private var PRESS_BG:Boolean=true;
		private var PRESS_BG_COLOR:Number=0x0000FF;
		private var PRESS_BG_ALPHA:Number=10;
		private var PRESS_LEFT_INDENT:Number=0;
	private var DISABLE_FONT:String="Tahoma";
		private var DISABLE_SIZE:Number=11;
		private var DISABLE_COLOR:Number=0xaaaaaa;
		private var DISABLE_BOLD:Boolean=false;
		private var DISABLE_ITALIC:Boolean=false;
		private var DISABLE_ALIGN:String="center";
		private var DISABLE_BG:Boolean=true;
		private var DISABLE_BG_COLOR:Number=0xEEEEEF;
		private var DISABLE_BG_ALPHA:Number=15;
		private var DISABLE_LEFT_INDENT:Number=0;

	private var text:String;
		
	function PressButtonViewClassic(){
	reset();
	
		var tfHoverFormat:TextFormat=new TextFormat(HOVER_FONT, HOVER_SIZE, HOVER_COLOR, HOVER_BOLD, HOVER_ITALIC,null,null,null, HOVER_ALIGN, HOVER_LEFT_INDENT);
		var tfNormalFormat:TextFormat=new TextFormat(NORMAL_FONT, NORMAL_SIZE, NORMAL_COLOR, NORMAL_BOLD, NORMAL_ITALIC,null,null,null, NORMAL_ALIGN, NORMAL_LEFT_INDENT);
		var tfPressFormat:TextFormat=new TextFormat(PRESS_FONT, PRESS_SIZE, PRESS_COLOR, PRESS_BOLD, PRESS_ITALIC,null,null,null, PRESS_ALIGN, PRESS_LEFT_INDENT);
		var tfDisableFormat:TextFormat=new TextFormat(DISABLE_FONT, DISABLE_SIZE, DISABLE_COLOR, DISABLE_BOLD, DISABLE_ITALIC,null,null,null, DISABLE_ALIGN, DISABLE_LEFT_INDENT);
		hoverFormat=new PressButtonViewItem();
		hoverFormat.setFormat(tfHoverFormat);
		hoverFormat.setBackgroundColor(HOVER_BG_COLOR);
		hoverFormat.setBackgroundAlpha(HOVER_BG_ALPHA);
		normalFormat=new PressButtonViewItem();
		normalFormat.setFormat(tfNormalFormat);
		normalFormat.setBackgroundColor(NORMAL_BG_COLOR);
		normalFormat.setBackgroundAlpha(NORMAL_BG_ALPHA);
		pressFormat=new PressButtonViewItem();
		pressFormat.setFormat(tfPressFormat);
		pressFormat.setBackgroundColor(PRESS_BG_COLOR);
		pressFormat.setBackgroundAlpha(PRESS_BG_ALPHA);
		disableFormat=new PressButtonViewItem();
		disableFormat.setFormat(tfDisableFormat);
		disableFormat.setBackgroundColor(DISABLE_BG_COLOR);
		disableFormat.setBackgroundAlpha(DISABLE_BG_ALPHA);
		
	}
	
	function getHoverFormat():PressButtonViewItem{
		return hoverFormat;
	}
	function getNormalFormat():PressButtonViewItem{
		return normalFormat;
	}
	function getPressFormat():PressButtonViewItem{
		return pressFormat;
	}
	function setHoverFormat(format:PressButtonViewItem):Void{
		hoverFormat=format;
	}
	function setNormalFormat(format:PressButtonViewItem):Void{
		normalFormat=format;
	}
	function setPressFormat(format:PressButtonViewItem):Void{
		pressFormat=format;
	}
	function getDisableFormat():PressButtonViewItem{
		return disableFormat;
	}
	function setDisableFormat(format:PressButtonViewItem):Void{
		disableFormat=format;
	}

	public function buttonHover(container:MovieClip):MovieClip{
		textField.setTextFormat(hoverFormat.getFormat());
		centerTextObject(textField, hoverFormat.getFormat());
		borde.setColor(getHoverFormat().getBackgroundColor());
		borde.setAlpha(getHoverFormat().getBackgroundAlpha());
		borde.refresh();
		var mc:MovieClip=ImageLoader.loadImage(container, hoverFormat.getImage());
		return container;
	};
	
	public function buttonPress(container:MovieClip):MovieClip{
		textField.setTextFormat(pressFormat.getFormat());
		centerTextObject(textField, pressFormat.getFormat());
		borde.setColor(getPressFormat().getBackgroundColor());
		borde.setAlpha(getPressFormat().getBackgroundAlpha());
		borde.refresh();
		var mc:MovieClip=ImageLoader.loadImage(container, pressFormat.getImage());
		return container;
	};
	
	public function buttonNormal(container:MovieClip):MovieClip{
		container.enabled=true;
		textField.setTextFormat(normalFormat.getFormat());
		centerTextObject(textField, normalFormat.getFormat());
		borde.setColor(getNormalFormat().getBackgroundColor());
		borde.setAlpha(getNormalFormat().getBackgroundAlpha());
		borde.refresh();
		var mc:MovieClip=ImageLoader.loadImage(container, normalFormat.getImage());
	return container;
	};
	
	public function buttonDisable(container:MovieClip):MovieClip{
	container.enabled=false;
	textField.setTextFormat(disableFormat.getFormat());
	centerTextObject(textField, disableFormat.getFormat());
	borde.setColor(getDisableFormat().getBackgroundColor());
	borde.setAlpha(getDisableFormat().getBackgroundAlpha());
	borde.refresh();
	var mc:MovieClip=ImageLoader.loadImage(container, disableFormat.getImage());
	return container;
	};
	
	public function tooltip(container:MovieClip, text:String):MovieClip{
	
	return container;
	};
	
	public function reset(){
	};

	public function border(container:MovieClip):MovieClip{
		borde= new Rectangulo(container,0,0,container.width-WIDTH_BORDER*2,container.height-WIDTH_BORDER*2,COLOR_BG,ALPHA_BG, false);
		borde.setBordes(WIDTH_BORDER, COLOR_BORDER, ALPHA_BORDER, true, true);
	return container;
	};

	public function setText(text:String){
		this.text=text;
		var mcItem:MovieClip=borde.getClip();
		mcItem.createTextField("textObject", mcItem.getNextHighestDepth(), MARGIN,0,mcItem._width-WIDTH_BORDER*2-MARGIN*2, calculateHeight(text));
		mcItem.textObject.selectable=false;
		//mcItem.textObject.background=true;
		//mcItem.textObject.backgroundColor=0xFFFFee;
		mcItem.textObject.html=HTML;
		mcItem.textObject.htmlText=this.text;
		textField=mcItem.textObject;
		buttonNormal(mcItem._parent);
	}
	
	private function centerTextObject(textObject:TextField, format:TextFormat):Void{
	textObject._y=(borde.getClip()._height)/2 - calculateBaseline(format, textObject.htmlText) - WIDTH_BORDER*2;
	}

	private function calculateHeight(text:String):Number{
	var metrics1= hoverFormat.getFormat().getTextExtent(text);
	var metrics2= normalFormat.getFormat().getTextExtent(text);
	var metrics3= pressFormat.getFormat().getTextExtent(text);
	HEIGHT_TEXT=Math.max(metrics1.textFieldHeight, Math.max(metrics2.textFieldHeight, metrics3.textFieldHeight));
	return HEIGHT_TEXT;
	}
	private function calculateBaseline(format:TextFormat, test):Number{
	var metrics= format.getTextExtent(test);
	BASELINE=metrics.textFieldHeight-metrics.ascent+metrics.descent/2; // los dos son por el hombro de la tipograf�a.
	return BASELINE;
	}

} 